package com.youan.serve.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.youan.pangu.security.bean.LoginInfor;
import com.youan.serve.controller.swagger.UserRoleControllerSwagger;
import com.youan.serve.dao.domain.UserRole;
import com.youan.serve.service.IUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author auto_code
 * @since 2020-03-04
 */
@Controller
@RequestMapping("/user-role")
public class UserRoleController implements UserRoleControllerSwagger{
    @Autowired
    private IUserRoleService userRoleService;

    @Override
    @PostMapping("/userRole")
    public UserRole addUserRole(UserRole userRole, LoginInfor loginInfor){
        userRoleService.remove(new LambdaQueryWrapper<UserRole>().eq(UserRole::getUserId, userRole.getUserId()));
        userRole.setUserRoleId(UUID.randomUUID().toString().replaceAll("-", ""));
        userRole.setOperator(loginInfor.getUsername());
        userRole.setOperateTime(new Date());
        userRoleService.save(userRole);
        return null;
    }

    @Override
    @PutMapping("/userRole")
    public UserRole updateUserRole(UserRole userRole, LoginInfor loginInfor){
        userRole.setOperator(loginInfor.getUsername());
        userRole.setOperateTime(new Date());
        userRoleService.updateById(userRole);
        return null;
    }

    @Override
    @GetMapping("/userRoles")
    public List<UserRole> selectAllUserRole(){
        return userRoleService.list();
    }

    @Override
    @DeleteMapping("/userRole/{userRoleId}")
    public void deleteUserRoleByUserRoleId(@PathVariable String userRoleId){
        userRoleService.removeById(userRoleId);
    }

    @Override
    @GetMapping("/userRole/{userRoleId}")
    public UserRole selectUserRoleByUserRoleId(@PathVariable String userRoleId){
        return userRoleService.getById(userRoleId);
    }
}

